<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../../">
  <title data-ice="title">src/toolbar/SectionTool.js | xeokit-bim-viewer</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<meta name="description" content="BIM viewer built on xeokit"><meta property="og:type" content="website"><meta property="og:url" content="https://github.com/xeokit/xeokit-bim-viewer"><meta property="og:site_name" content="xeokit-bim-viewer"><meta property="og:title" content="xeokit-bim-viewer"><meta property="og:image" content="./images/logo.jpg"><meta property="og:description" content="BIM viewer built on xeokit"><meta property="og:author" content="http://xeolabs.com"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="xeokit-bim-viewer"><meta property="twitter:description" content="BIM viewer built on xeokit"><meta property="twitter:image" content="./images/logo.jpg"></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./" style="display: flex; align-items: center;"><img src="./image/brand_logo.jpg" style="width:34px;"></a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
<a style="position:relative; top:3px;" href="https://github.com/xeokit/xeokit-bim-viewer"><img width="20px" src="./image/github.png"></a></header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/BIMViewer.js~BIMViewer.html">BIMViewer</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#collision">collision</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/collision/ObjectsKdTree3.js~ObjectsKdTree3.html">ObjectsKdTree3</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#server">server</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/server/Server.js~Server.html">Server</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">src/toolbar/SectionTool.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import {Controller} from &quot;../Controller.js&quot;;
import {SectionToolContextMenu} from &quot;./../contextMenus/SectionToolContextMenu.js&quot;;
import {math, SectionPlanesPlugin} from &quot;@xeokit/xeokit-sdk/dist/xeokit-sdk.es.js&quot;;

/** @private */
class SectionTool extends Controller { // XX

    constructor(parent, cfg) {

        super(parent, cfg);

        if (!cfg.buttonElement) {
            throw &quot;Missing config: buttonElement&quot;;
        }

        if (!cfg.menuButtonElement) {
            throw &quot;Missing config: menuButtonElement&quot;;
        }

        this._buttonElement = cfg.buttonElement;
        this._counterElement = cfg.counterElement;
        this._menuButtonElement = cfg.menuButtonElement;
        this._menuButtonArrowElement = cfg.menuButtonArrowElement;

        this._sectionPlanesPlugin = new SectionPlanesPlugin(this.viewer, {});

        this._sectionToolContextMenu = new SectionToolContextMenu({
            sectionPlanesPlugin: this._sectionPlanesPlugin,
            hideOnMouseDown: false,
            hideOnAction: false
        });

        this._sectionPlanesPlugin.setOverviewVisible(false);

        this.on(&quot;enabled&quot;, (enabled) =&gt; {
            if (!enabled) {
                this._buttonElement.classList.add(&quot;disabled&quot;);
                if (this._counterElement) {
                    this._counterElement.classList.add(&quot;disabled&quot;);
                }
                this._menuButtonElement.classList.add(&quot;disabled&quot;);
                this._menuButtonArrowElement.classList.add(&quot;disabled&quot;);
            } else {
                this._buttonElement.classList.remove(&quot;disabled&quot;);
                if (this._counterElement) {
                    this._counterElement.classList.remove(&quot;disabled&quot;);
                }
                this._menuButtonElement.classList.remove(&quot;disabled&quot;);
                this._menuButtonArrowElement.classList.remove(&quot;disabled&quot;);
            }
        });

        this.on(&quot;active&quot;, (active) =&gt; {
            if (active) {
                this._buttonElement.classList.add(&quot;active&quot;);
                if (this._counterElement) {
                    this._counterElement.classList.add(&quot;active&quot;);
                }
                this._menuButtonElement.classList.add(&quot;active&quot;);
                this._menuButtonArrowElement.classList.add(&quot;active&quot;);
            } else {
                this._buttonElement.classList.remove(&quot;active&quot;);
                if (this._counterElement) {
                    this._counterElement.classList.remove(&quot;active&quot;);
                }
                this._menuButtonElement.classList.remove(&quot;active&quot;);
                this._menuButtonArrowElement.classList.remove(&quot;active&quot;);
            }
        });

        this.on(&quot;active&quot;, (active) =&gt; {
            if (!active) {
                this._sectionPlanesPlugin.hideControl();
            }
        });

        this._buttonElement.addEventListener(&quot;click&quot;, (e) =&gt; {
            if (!this.getEnabled()) {
                return;
            }
            if (e.target === this._menuButtonElement || e.target.parentNode === this._menuButtonElement) {
                return;
            }
            const active = this.getActive();
            this.setActive(!active);
            e.preventDefault();
        });

        document.addEventListener(&quot;mousedown&quot;, (e) =&gt; {

            if (e.target.classList.contains(&quot;xeokit-context-menu-item&quot;)) {
                // Allow click on menu item
                return;
            }

            if (e.target === this._menuButtonElement || e.target.parentNode === this._menuButtonElement) {
                e.preventDefault();
                if (this._sectionToolContextMenu.shown) {
                    this._sectionToolContextMenu.hide();
                } else {
                    this._sectionToolContextMenu.context = {
                        bimViewer: this.bimViewer,
                        viewer: this.viewer,
                        sectionTool: this
                    };

                    const rect = this._menuButtonElement.getBoundingClientRect();

                    this._sectionToolContextMenu.show(rect.left, rect.bottom + 5);
                }
            } else {
                this._sectionToolContextMenu.hide();
            }
        });

        this._sectionToolContextMenu.on(&quot;shown&quot;, () =&gt; {
            this._menuButtonArrowElement.classList.remove(&quot;xeokit-arrow-down&quot;);
            this._menuButtonArrowElement.classList.add(&quot;xeokit-arrow-up&quot;);
        });

        this._sectionToolContextMenu.on(&quot;hidden&quot;, () =&gt; {
            this._menuButtonArrowElement.classList.remove(&quot;xeokit-arrow-up&quot;);
            this._menuButtonArrowElement.classList.add(&quot;xeokit-arrow-down&quot;);
        });

        this.bimViewer.on(&quot;reset&quot;, () =&gt; {
            this.clear();
            this.setActive(false);
        });

        this.viewer.scene.on(&quot;sectionPlaneCreated&quot;, () =&gt; {
            this._updateSectionPlanesCount();
        });

        this.viewer.scene.on(&quot;sectionPlaneDestroyed&quot;, () =&gt; {
            this._updateSectionPlanesCount();
        });

        this._initSectionMode();
    }

    _initSectionMode() {

        this.viewer.scene.input.on(&quot;mouseclicked&quot;, (coords) =&gt; {

            if (!this.getActive() || !this.getEnabled()) {
                return;
            }

            const pickResult = this.viewer.scene.pick({
                canvasPos: coords,
                pickSurface: true  // &lt;&lt;------ This causes picking to find the intersection point on the entity
            });

            if (pickResult) {

                const sectionPlane = this._sectionPlanesPlugin.createSectionPlane({
                    pos: pickResult.worldPos,
                    dir: math.mulVec3Scalar(pickResult.worldNormal, -1)
                });

                this._sectionPlanesPlugin.showControl(sectionPlane.id);
            }
        });

        this._updateSectionPlanesCount();
    }

    _updateSectionPlanesCount() {
        if (this._counterElement) {
            this._counterElement.innerText = (&quot;&quot; + this.getNumSections());
        }
    }

    getNumSections() {
        return Object.keys(this.viewer.scene.sectionPlanes).length;
    }

    clear() {
        this._sectionPlanesPlugin.clear();
        this._updateSectionPlanesCount();
    }

    flipSections() {
        this._sectionPlanesPlugin.flipSectionPlanes();
    }

    enableSections() {
        const sectionPlanes = this.viewer.scene.sectionPlanes;
        for (let id in sectionPlanes) {
            const sectionPlane = sectionPlanes[id];
            sectionPlane.active = true;
        }
    }

    disableSections() {
        const sectionPlanes = this.viewer.scene.sectionPlanes;
        for (let id in sectionPlanes) {
            const sectionPlane = sectionPlanes[id];
            sectionPlane.active = false;
        }
    }

    hideControl() {
        this._sectionPlanesPlugin.hideControl();
    }

    destroy() {
        this._sectionPlanesPlugin.destroy();
        this._sectionToolContextMenu.destroy();
        super.destroy();
    }
}

export {SectionTool};</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
